package com.cmhi.softmbh.service;

import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Handler;
import android.os.IBinder;

import androidx.annotation.Nullable;

import com.cmhi.softcoredata.tools.CmhiInterface;
import com.cmhi.softmbh.BuildConfig;
import com.cmhi.softmbh.cache.StaticConst;
import com.cmhi.softmbh.entity.Entity;
import com.cmhi.softmbh.provider.DBUtil;
import com.softmbh.kellyplatform.util.AppUtils;
import com.softmbh.kellyplatform.util.MacUtils;

import titan.sdk.android.TitanSDKCore;

public class BmService extends Service {
    private SharedPreferences sharedPreferences;

    Handler handler = new Handler();
    Runnable runnable = new Runnable() {
        @Override
        public void run() {
            test();
        }
    };
    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        handler.post(runnable);
        return Service.START_STICKY;
    }


     void test(){
        AppUtils.debugLog("BmService onStartCommand");
        handler.postDelayed(runnable,2000);
    }
    @Override
    public void onCreate() {
        super.onCreate();
        AppUtils.debugLog("BmService onCreate");
        if (sharedPreferences == null) {
            sharedPreferences = getSharedPreferences(StaticConst.SP_FILE_NAME, Context.MODE_PRIVATE);
        }
        int tiTianId = sharedPreferences.getInt(StaticConst.SP_KEY_TITIAN_ID, 0);
        if (tiTianId <= 0) {
            tiTianId = 0x0001002D;
        }
        TitanSDKCore.setToken(tiTianId); // TOKEN: MUST set to yourself's token
        TitanSDKCore.setQuota(StaticConst.QUOTA);
        TitanSDKCore.start(this.getApplicationContext());
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        AppUtils.debugLog("BmService onDestroy");
        TitanSDKCore.stop();
    }

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return cmhiInterface;
    }

    private CmhiInterface.Stub cmhiInterface = new CmhiInterface.Stub() {

        @Override
        public String getEPGToken() {
            return DBUtil.query(Entity.APP_TOKEN);
        }

        @Override
        public String getStbId() {
            return DBUtil.query(Entity.VIRTUAL_STBID);
        }

        @Override
        public String getSN() {
            return DBUtil.query(Entity.STBID);
        }

        @Override
        public String getVirtualMac() {
            return DBUtil.query(Entity.VIRTUAL_MAC);
        }

        @Override
        public String getUserAccount() {
            return DBUtil.query(Entity.ACCOUNT);
        }

        @Override
        public String getUserPhone() {
            return DBUtil.query(Entity.ACCOUNT);
        }

        @Override
        public String getUserType() {
            return "NORMAL";
        }

        @Override
        public String getProvinceCode() {
            return DBUtil.query(Entity.PROVINCE_ID);
        }

        @Override
        public String getCityCode() {
            return "";
        }

        @Override
        public String getAreaCode() {
            return DBUtil.query(Entity.AREA_CODE);
        }

        @Override
        public String getDistrictName() {
            return "";
        }

        @Override
        public String getBroadbandAccount() {
            return "";
        }

        @Override
        public String getLauncher() {
            return DBUtil.query(Entity.LAUNCHER_ACTION);
        }

        @Override
        public String getSettingAction() {
            return DBUtil.query(Entity.SETTING_ACTION);
        }

        @Override
        public String getPlayerVersion() {
            return "";
        }

        @Override
        public String getDeviceMediaInfo() {
            return DBUtil.query(Entity.DEVICE_MEDIA_INFO);
        }

        @Override
        public String getBIZMoudel() {
            return "OTT";
        }

        @Override
        public String getUserState() {
            return DBUtil.query(Entity.USER_STATE);
        }

        @Override
        public String getTVMac() {
            return MacUtils.getMac();
        }

        @Override
        public String getTVID() {
            return "";
        }

        @Override
        public String getOTTTerminalType() {
            return BuildConfig.OTT_TERMINAL_TYPE;
        }

        @Override
        public String getOTTTerminalBrand() {
            return DBUtil.query(Entity.EN_BRAND);
        }

        @Override
        public String getChannelType() {
            return BuildConfig.CHANNEL_TYPE;
        }

        @Override
        public String getIsSoftTerminal() {
            return "true";
        }

        @Override
        public String getExtendValue(String s) {
            switch (s) {
                default:
                    return "";
            }
        }
    };
}
